The present invention relates to objects defined within an object-oriented computer programming system. More specifically, the present invention relates to a method and apparatus for creating objects within an object-oriented computer programming system.
The recent proliferation of ever smaller and more capable computing devices has lead to the use of platform-independent programming languages on these smaller devices. Platform-independent programming languages allow the creation of programs that can be compiled to a set of platform-independent codes, which can be executed on a variety of computing devices. Many of these computing devices have interpreters that execute these platform-independent codes. The JAVA(trademark) programming language is an example of a platform-independent programming language and JAVA bytecodes are an example of platform-independent codes.
The terms JAVA, JVM and JAVA VIRTUAL MACHINE are registered trademarks of SUN Microsystems, Inc. of Palo Alto, Calif.
Many platform-independent programming languages, including JAVA, are object-oriented programming languages. An object is typically an instance of a class, which usually has data and methods associated with it. Note that methods are functions that are used to manipulate data.
These objects may also have a meta-class instance associated with them. For instance, the JAVA virtual machine (VM) creates a java.lang.Class instance for each class that is loaded by the VM. The meta-class instance is created in an area of memory called the heap and contains information common to all objects of that class. Information within the meta-class instance includes, but is not limited to, the number of variables associated with each instance of the class and the number of methods associated with the class.
Some drawbacks to creating meta-class instances on the heap are that these objects can take up a large amount of space on the heap and that a program may create many of these objects. This can be a problem for smaller computing devices, which have limited storage space within the heap that can be quickly consumed by the meta-class instances. Also, with many meta-class instances stored within the heap, garbage collection takes more of the computing devices resources, thereby slowing the overall execution of the program.
Under many circumstances, creation of the meta-class instance is not required. As an example, the JAVA specification requires the creation of a meta-class instance only when the java.lang.Object::getClass"Ovalhollow" method is explicitly invoked. Hence, creating the meta-class instance wastes resources under many circumstances.
What is needed is a method and apparatus for eliminating this waste of resources while maintaining the integrity of the platform-independent programming system.
One embodiment of the present invention provides a system for creating objects in a virtual machine. The system operates by receiving a request to create an object within an object-oriented programming system. Upon receiving the request, if a meta-class instance associated with the object does not already exist, the system creates a structure to represent the meta-class instance in a data space that is not subject to garbage collection. If an explicit instruction to create the meta-class instance is detected, the system creates the meta-class instance within a garbage-collected data space.
In one embodiment of the present invention, the structure is created by directly executing instructions in a native language of a computing device, without having to convert platform-independent instructions into instructions in the native language of the computing device.
In one embodiment of the present invention, the system initializes a variable within the structure with a value. After initializing the variable with a value, the system can change the value of the variable. The system can also invoke an executable method of the object. In this embodiment, the acts of initializing a variable, changing the value of the variable, and invoking an executable method of the object involve executing the native language of the computing device.
In one embodiment of the present invention, the system destroys the structure using the native language of the computing device when the object is no longer needed.
In one embodiment of the present invention, the meta-class instance is created by executing instructions within an interpreted language.
In one embodiment of the present invention, the system initializes a variable within the meta-class instance with a value. After initializing the variable with a value, the system can change the value of the variable. The system can also invoke an executable method of the meta-class instance. In this embodiment, the acts of initializing a variable, changing the value of the variable, and invoking an executable method of the object include converting platform-independent instructions into native language instructions of a computing device.
In one embodiment of the present invention, the system allows the meta-class instance to be deleted by a garbage collector when the meta-class instance is no longer needed.
In one embodiment of the present invention, the meta-class instance includes a java.lang.Class instance.